package com.tutu.service.impl;

import com.tutu.core.Constant;
import com.tutu.core.api.SystemApiUrls;
import com.tutu.entity.Module;
import com.tutu.service.RoleModuleService;
import com.tutu.utils.BaseUtil;
import com.tutu.utils.HttpRequestUtil;
import com.tutu.utils.JsonUtil;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * roleModuleServiceImpl
 * Created by 奕恒 on 2017/2/20 21:54.
 */
@Service
public class RoleModuleServiceImpl implements RoleModuleService {

    @Override
    public List<Module> findListByMasterId(Integer masterId) {
        Map<String, Object> map;
        List<Module> moduleList = new ArrayList<>();
        Module module;

        for (Object o : HttpRequestUtil.getListData(SystemApiUrls.base_prefix + SystemApiUrls.module_findListByMasterId, "masterId=" + masterId)) {
            map = JsonUtil.json2Map(o);

            module = new Module();

            module.setId(Integer.parseInt(map.get("id") + ""));
            module.setName(map.get("name") + "");
            module.setLiName(map.get("liName") + "");
            module.setUrl(map.get("url") + "");
            module.setDescription(map.get("description") + "");

            moduleList.add(module);
        }

        return moduleList;
    }

    @Override
    public List<Module> findListByRoleId(Integer roleId) {
        List<Module> list = new ArrayList<>();
        Module module;
        Map<String, Object> map;

        for (Object o : HttpRequestUtil.getListData(SystemApiUrls.base_prefix + SystemApiUrls.module_findModuleListToChangeByRoleId, "roleId=" + roleId)) {
            if (null == o || o.equals("null")) continue;

            map = JsonUtil.json2Map(o);

            module = new Module();

            module.setId(Integer.parseInt(map.get("id") + ""));
            module.setName(map.get("name") + "");
            module.setStatus(Integer.parseInt(map.get("status") + ""));

            list.add(module);
        }

        return list;
    }

    @Override
    public Map<String, Object> savePower(HttpServletRequest request, Integer id, String ids) {
        LogInfoServiceImpl.saveLogInfo(request, "编辑角色权限信息，ID：" + id + "，关联模块IDS：" + ids);

        try {
            return HttpRequestUtil.getCompleteData(SystemApiUrls.base_prefix + SystemApiUrls.role_updateModuleRelation, "roleId=" + id + "&moduleIds=" + ids);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return BaseUtil.defaultErrorInfo(null);
    }
}